// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.editors;

import java.util.List;

import org.dengues.commons.utils.FileUtils;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.IWarehousePluginService;
import org.dengues.core.components.IComponentsFactory;
import org.dengues.core.components.IXMLComponent;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.model.warehouse.Storage;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.ui.part.FileEditorInput;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class AbstractEditorInput extends FileEditorInput {

    protected IWarehouseNode warehouseNode;

    protected IWarehouseNode parentNode;

    private final IComponentsFactory factory = DenguesCorePlugin.getDefault().getDesignerCoreService().getComponentsFactory();;

    private IPath path;

    private final String editorUniqueName;

    protected Storage emfStorage;

    private ENodeCategoryName name;

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractEditorInput constructor comment.
     * 
     * @param objName
     * @param name
     * @param path
     * @param id
     */
    public AbstractEditorInput(String objName, ENodeCategoryName name, IPath path, String id) {
        this(WarehouseResourceFactory.getFileFromEMF(objName, name, path, id), objName, name, path);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractEditorInput constructor comment.
     * 
     * @param file
     */
    public AbstractEditorInput(IFile file, String objName, ENodeCategoryName name, IPath path) {
        this(file, objName, name);
        this.path = path;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractEditorInput constructor comment.
     * 
     * @param file
     */
    public AbstractEditorInput(IFile file, String objName, ENodeCategoryName name) {
        super(file);
        Storage loadStorage = WarehouseResourceFactory.loadStorage(name, file);
        if (loadStorage != null) {
            setEMFStorage(loadStorage);
        }
        this.editorUniqueName = GEFEditorUtils.getEditorUniqueName(objName, name);
        this.name = name;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.FileEditorInput#getName()
     */
    @Override
    public String getName() {
        if (getEMFStorage() != null) {
            return getEMFStorage().getName();
        }
        return super.getName();
    }

    /**
     * Getter for warehouseNode.
     * 
     * @return the warehouseNode
     */
    public IWarehouseNode getWarehouseNode() {
        return this.warehouseNode;
    }

    public void setParentNode(IWarehouseNode parentNode) {
        this.parentNode = parentNode;
    }

    protected IXMLComponent getComponent(String compName) {
        List<IXMLComponent> components = factory.getComponents();
        for (IXMLComponent component : components) {
            if (component.getName().equals(compName)) {
                return component;
            }
        }
        return null;
    }

    @Override
    public IPath getPath() {
        if (this.path == null) {
            IPath path2 = super.getPath();
            path2 = FileUtils.getStoragePath(path2, name.getName());
            return path2;
        }
        return this.path;
    }

    /**
     * Getter for editorUniqueName.
     * 
     * @return the editorUniqueName
     */
    public String getEditorUniqueName() {
        return this.editorUniqueName;
    }

    /**
     * Getter for storage.
     * 
     * @return the storage
     */
    public Storage getEMFStorage() {
        return this.emfStorage;
    }

    /**
     * Sets the storage.
     * 
     * @param storage the storage to set
     */
    public void setEMFStorage(Storage storage) {
        this.emfStorage = storage;
    }

    /**
     * Sets the warehouseNode.
     * 
     * @param warehouseNode the warehouseNode to set
     */
    public void setWarehouseNode(IWarehouseNode warehouseNode) {
        if (warehouseNode != null) {
            this.warehouseNode = warehouseNode;
        } else {
            IWarehousePluginService warehouseService = DenguesCorePlugin.getDefault().getWarehousePluginService();
            this.warehouseNode = warehouseService.getWarehouseViewFactory().createWarehouseNodeFromEMF(name, parentNode,
                    emfStorage);
        }
    }
}
